#include "bookmark.h"

Bookmark::Bookmark()
{
    m_manga = 0;
}
Bookmark::~Bookmark()
{
    delete m_manga;
}
Bookmark::Bookmark(Manga * m,QString chap, QString page,int id,bool fin_atteinte)
{
    m_manga = m;
    m_chapitre = chap;
    m_page = page;
    m_fin_atteinte = fin_atteinte;
    m_id = id;
}
Bookmark::Bookmark(int mangaid,QString nom, QString adr,QString chap,QString page,int id, bool fin_atteinte)
{
    m_manga = new Manga(mangaid,nom,adr);
    m_chapitre = chap;
    m_page = page;
    m_fin_atteinte = fin_atteinte;
    m_id = id;
}
void Bookmark::setManga(Manga * m)
{
    delete m_manga;
    m_manga = m;
}
void Bookmark::setManga(int mangaid,QString nom, QString adr)
{
    delete m_manga;
    m_manga = new Manga(mangaid,nom,adr);
}
void Bookmark::setChapitre(QString chap)
{
    m_chapitre = chap;
}
void Bookmark::setPage(QString page)
{
    m_page = page;
}
void Bookmark::setFinAtteinte(bool b)
{
    m_fin_atteinte = b;
}
Manga * Bookmark::manga()
{
    return m_manga;
}
QString Bookmark::chapitre()
{
    return m_chapitre;
}
QString Bookmark::page()
{
    return m_page;
}
bool Bookmark::finAtteinte()
{
    return m_fin_atteinte;
}
bool Bookmark::serialize(QXmlStreamWriter * w)
{
    w->writeStartElement("Bookmark");
    w->writeAttribute("id",QString::number(m_id));
    w->writeAttribute("chapitre",m_chapitre);
    w->writeAttribute("page",m_page);
    w->writeAttribute("finAtteinte",m_fin_atteinte?"true":"false");
    m_manga->serialize(w);
    w->writeEndElement();
    return true;
}
bool Bookmark::deserialize(QXmlStreamReader * r)
{
    QXmlStreamAttributes a = r->attributes();
    m_id = ((QXmlStreamAttribute)a.at(0)).value().toString().toInt(0,10);
    m_chapitre = ((QXmlStreamAttribute)a.at(1)).value().toString();
    m_page = ((QXmlStreamAttribute)a.at(2)).value().toString();
    m_fin_atteinte = ((QXmlStreamAttribute)a.at(2)).value().toString().compare("true") == 0;
    delete m_manga;
    while(!r->atEnd() && !(r->isStartElement() && r->name().toString() == "Manga"))
    {
	r->readNext();
    }
    if(!r->atEnd())
    {
	m_manga = new Manga();
	m_manga->deserialize(r);
	return true;
    }
    else
    {
	return false;
    }
}
QStringList Bookmark::getHeaders()
{
    return Resource::getHeaders() << "Manga" << "Chapitre" << "Page" << "Fin Atteinte";
}
QList<QTableWidgetItem*> Bookmark::getItems()
{
    return Resource::getItems() << new QTableWidgetItem(manga()->nom()) << new QTableWidgetItem(chapitre()) << new QTableWidgetItem(page()) << new QTableWidgetItem(finAtteinte() == true?"oui":"non");
}
